<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\supplier\model;

use think\Model;
use think\Db;

/**
 * 后台操作日志
 * @package app\supplier\model
 */
class CountryModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'e_country';
	
	/**
     * 获取国家列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getCountryList($input)
	{
		$query = Db::name($this->table)->where('country_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('country_code|country_name','like','%'.$input['keyword'].'%');
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=>$list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}
	
	/**
     * 获取车型库列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getFieldCountryList($input)
	{
		$query = Db::name('e_field_country')->where('rela_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('country_code|country_name','like','%'.$input['keyword'].'%');
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=>$list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}
	
	/**
     * 获取领域关联的国家区域
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getCountryRela($where)
	{
		return Db::name('e_field_country')->where($where)->select();
	}
	
	/**
     * 更新国家与领域的关联关系
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function insertCountryRela($input)
	{
		$country_id = $input['id'];
		$field_id_arr = explode('|',$input['field_id']);
		$where = array();
		$where['country_id'] = $country_id;
		$country_rows = Db::name('e_field_country')->where($where)->select();
		if(!empty($country_rows)){
			$field_id_arr = array_flip($field_id_arr);
			foreach($country_rows as $_c){
				if(isset($field_id_arr[$_c['field_id']])){
					unset($field_id_arr[$_c['field_id']]);
				}
			}
			if(!empty($field_id_arr)){
				$field_id_arr = array_flip($field_id_arr);
				$insert_data = array();
				foreach($field_id_arr as $_f){
					array_push(
							$insert_data,
							array(
								'field_id'=>$_f,
								'country_id'=>$country_id,
								'create_time'=>time(),
								'update_time'=>time()
							)
						);
				}
				return Db::name('e_field_country')->insertAll($insert_data);
			}else{
				return true;
			}
		}else{
			if(!empty($field_id_arr)){
				$insert_data = array();
				foreach($field_id_arr as $_f){
					array_push(
							$insert_data,
							array(
								'field_id'=>$_f,
								'country_id'=>$country_id,
								'create_time'=>time(),
								'update_time'=>time()
							)
						);
				}
				return Db::name('e_field_country')->insertAll($insert_data);
			}else{
				return true;
			}
		}
	}
}
